#include <iostream>

template <typename T>
void Swap(T &a, T &b)
{
    T tmp = a;
    a = b;
    b = tmp;
}

template <typename T>
void Sort(T *a, int size)
{
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size - 1 - i; j++)
        {
            if (a[j] > a[j + 1])
            {
                Swap<T>(a[j], a[j + 1]); // 模版函数中使用模版函数
            }
        }
    }
}

struct Student
{
    int id;
    std::string name;
    bool operator>(const Student &s)
    {
        return id > s.id;
    }
};

std::ostream &operator<<(std::ostream &os, const Student &s)
{
    std::cout << s.id << " " << s.name << std::endl;
    return os;
}

// 特化:模版具体化 (没有模版声明，直接定义具体类型)
// 显式声明模版的数据类型
template <>
void Swap<Student>(Student &a, Student &b)  // 用特化时，<Student>需要加上，否则主函数加<Student>，会调用上面那个通用模版
{
    std::cout<<"特化模版"<<std::endl;
    std::string tmp = a.name;
    a.name = b.name;
    b.name = tmp;
}

int main()
{
    // Student a[3] =
    //     {
    //         {2, "张三"},
    //         {1, "李四"},
    //         {3, "王五"}
    //     };
    // Sort(a, 3);
    // for (int i = 0; i < 3; i++)
    // {
    //     std::cout << a[i] <<std::endl;
    // }

    Student s1 = {1, "张三"};
    Student s2 = {2, "李四"};
    Swap(s1, s2);
    std::cout << s1 << std::endl;
    std::cout << s2 << std::endl;

    return 0;
}